#!/usr/bin/python
# -*- coding: UTF-8 -*-
import operator
import time
import numpy as np
import pandas as pd
import os
import pymongo
from config import *
from bson import json_util as jsonb
import json
client = pymongo.MongoClient(MONGO_URL)
from shutil import copyfile
from stock.indus import IndusBean

db = client["stocks"]
table_stocks = db["weekstocks"]
table_indus = db["indus"]
table_exchange = db["exchange"]


class StockAlayse(object):
    def __init__(self):
        self.dir = "C:\\Users\\Administrator\\Desktop\\stock"
        self.out_dir = "C:\\Users\\Administrator\\Desktop\\stock"
        self.industry = []  # 行业
        self.concepts_industry = []  # 概念
        self.without = ["转融券标的", "融资融券", "深股通", "沪股通"]

    # 获取概念统计数据
    def __result_concepts_industry(self, data):
        all = []
        final_result = {}
        results = data["所属概念"].values
        for result in results:
            for r in result.split(";"):
                if r in self.without:
                    continue
                if r in final_result:
                    final_result[r] = final_result.get(r) + 1
                else:
                    final_result[r] = 1
        sort_result = sorted(final_result.items(), key=operator.itemgetter(1))
        for key, value in sort_result:
            print(key, ":", value)
        df2 = pd.DataFrame(sort_result)
        df2 = df2[df2[1] > 1]
        print(df2)
        df2 = pd.DataFrame(sort_result)
        df2.to_excel(self.out_dir+"\\week概念.xlsx")
        print('概念数据处理完成')

    # bar = pc.Bar(title)
    # bar.add("", df2[0], df2[1], mark_point=["max"], is_convert=True)
    # bar.render(title+".html")
    # 获取行业统计数据
    def __result_industry(self, data):
        all = []
        final_result = {}
        results = data["所属同花顺行业"].values
        for result in results:
            for r in result.split("-"):
                if r in self.without:
                    continue
                if r in final_result:
                    final_result[r] = final_result.get(r) + 1
                else:
                    final_result[r] = 1
        sort_result = sorted(final_result.items(), key=operator.itemgetter(1))
        for key, value in sort_result:
            print(key, ":", value)
        df2 = pd.DataFrame(sort_result)
        df2.to_excel(self.out_dir+"\\week行业.xlsx")
        print('行业数据处理完成')

    def __filter_stock_code(self, code):
        result = code.split(".")
        if result:
            result = result[0]
        else:
            result = code
        return result.strip()

    def __filter_indus_name(self, str):
        result = str.split("-")
        return result

    def __filter_concetps_name(self, str):
        result = str.split(";")
        return result

    def __saveToMongo(self, stock):
        queryArgs = {"code": stock["code"]}
        try:
            result = table_stocks.find_one(queryArgs)
            print("查询结果", result, queryArgs)
            if result:
                print("update_stock")
                self.__update_stock(stock)
            else:
                print("insert stock")
                table_stocks.insert_one(stock)
        except Exception as e:
            print(e)

    def __update_stock(self, stock):

        table_stocks.update({"code": stock["code"]}, {"$set": {"tag": stock["tag"]}})
        print("update success")

    def get_stock_mongodb(self):
        print("开始处理股票数据到mongodb")
        timestr = time.strftime("%Y-%m-%d", time.localtime());
        file_name = self.dir + "\\" + timestr + ".xls"
        newfile = self.dir + "\\" + timestr + ".html"

        print(file_name, newfile)
        os.rename(file_name, newfile)

        html = pd.read_html(newfile)
        os.rename(newfile, file_name)
        data = html[0]

        columns_names = data[0:1]
        cnn = np.array(columns_names)[0]
        data.columns = cnn
        data = data.drop(labels=0)
        self.__result_industry(data)
        self.__result_concepts_industry(data)

        data["股票代码"] = data["股票代码"].apply(self.__filter_stock_code)
        data["所属同花顺行业"] = data["所属同花顺行业"].apply(self.__filter_indus_name)
        data["所属概念"] = data["所属概念"].apply(self.__filter_concetps_name)
        data = data.loc[:, ["股票代码", "股票简称", "所属概念", "所属同花顺行业"]]
        for index, row in data.iterrows():
            stock = {
                "code": row["股票代码"],
                "name": row["股票简称"],
                "tag": row["所属概念"] + row["所属同花顺行业"],
                "des": ""
            }
            print(stock)
            self.__saveToMongo(stock)
            inMongodbIndus=self.__stock_belong_indus_mongodb(stock["tag"])
            if inMongodbIndus:
                indusBean=IndusBean(self.query_indus_byname(inMongodbIndus))
                g=indusBean.get_g()
                sample=indusBean.get_sample()
                indusBean.set_g(g+1)
                stock_name=row["股票简称"]
                if stock_name not in sample:
                    sample.append(stock_name)
                indus_json=indusBean.to_json()
                print(indus_json)
                self.__update_indus(indusBean.to_json())






    def __getindus_forCode(self):
        pass

    #当前股票是否属于数据库行业
    def __stock_belong_indus_mongodb(self,rows):
        indus=list(self.query_all_indus())
        for indu in indus:
            if indu["name"] in rows:
                print(indu["name"])
                return indu["name"]

        return None




    def __filter_stock_code(self, stock):
        if stock != None:
            return stock.split(".")[0]

    def __write(self, stock):
        output_stock_path = self.out_dir + "\\stock.txt"
        with open(output_stock_path, "a") as f:
            f.write(stock)
            f.write("\n\r")

    # 写入股票代码 供东方财富使用
    def __write_stock_code_file(self, data):

        data["股票代码"] = data["股票代码"].apply(self.__filter_stock_code)
        allstocks = list(data["股票代码"])
        print(allstocks)
        for stock in allstocks:
            self.__write(stock)

    #查询行业股票
    def query_indus_stock(self, tag):
        print("query_indus_stockt", tag)
        filter_find = {'tag': {'$in': [tag]}}
        results = table_stocks.find(filter_find)

        return results

    #删除股票通过行业
    def delete_stock_byTag(self,tag):
        filter_find = {'tag': {'$in': [tag]}}
        code=table_stocks.remove(filter_find)
        print(code)

    #删除股票通过代码
    def delete_stock_byCode(self,code):
        filter_find = {'code': code}
        code=table_stocks.remove(filter_find)
        print(code)

    #删除股票通过代码
    def delete_stock_byName(self,name):
        filter_find = {'name': name}
        code=table_stocks.remove(filter_find)
        print(code)

    #增加行业
    def add_indus(self,indus):
        print("add_indus",indus)
        queryArgs = {"name": indus["name"]}
        try:
            result = table_indus.find_one(queryArgs)
            print("行业查询结果", result, queryArgs)
            if result:
                print("update_indus")
                self.__update_indus(indus)
            else:
                print("insert indus")
                table_indus.insert_one(indus)
        except Exception as e:
            print(e)

    #更新行业
    def __update_indus(self, indus):
        table_indus.update({"name": indus["name"]}, {"$set": indus})
        print("update success")
    #查询行业通过行业名
    def query_indus_byname(self,name):
        queryArgs = {"name": name}
        result = table_indus.find_one(queryArgs)
        return result

    def query_all_indus(self):
        queryArgs = {}
        projectionFields = {'_id': False}  # 用字典指定
        result = table_indus.find(queryArgs,projectionFields)
        return result


    def save_exchange_record(self,exchange):
        table_exchange.insert_one(exchange)








